<?php

	/**
    * @author trungdt
    * @since Nov 1, 2011
    * 
	* Change logs
	* Nov 1, 2011 - trungdt - Change Edit action for new template
	*/

	if (!defined('EXPONENT')) exit('');

	$item = null;
	$iloc = null;
	if (isset($_GET['id'])) {
		$item = $db->selectObject('calendar','id=' . intval($_GET['id']));	
		if ($item)
			$loc = unserialize($item->location_data);
	}

	if (($item == null && exponent_permissions_check('post',$loc)) ||
	($item != null && exponent_permissions_check('edit',$loc))
	) {
		$form = calendar::form($item, $loc);
		$form->meta('action','save');
		$form->location($loc);

		$config = $db->selectObject('calendarmodule_config',"location_data='".serialize($loc)."'");
		if (!$config)
			$config->enable_categories = 0;

		$i18n = exponent_lang_loadFile('modules/calendarmodule/actions/edit.php');

		if ($config->enable_categories == 1) {
			$ddopts = array();
			foreach ($db->selectObjects('category',"location_data='".serialize($loc)."'") as $opt) {
				$ddopts[$opt->id] = $opt->name;
			}
			uasort($ddopts,'strnatcmp');

			if (!isset($item->category_id)) $item->category_id = null;
			$form->registerAfter('eventend','category',$i18n['categories'],new dropdowncontrol($item->category_id,$ddopts));
			$form->registerBefore('category', null, '', new htmlcontrol('<br />'));
		}

		$template = new template('calendarmodule','_form_edit',$loc);
		$template->assign('form_html',$form->toHTML());
		$template->assign('is_edit',($item == null ? 0 : 1));

		$template->output();
	} else {
		echo SITE_403_HTML;
	}

?>
